package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.ArticleReplyMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.ArticleReply;
import com.example.forum.services.IArticleReplyService;
import com.example.forum.services.IArticleService;
import com.example.forum.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpl implements IArticleReplyService {

    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource
    private IArticleService articleService;

    //根据帖子id查询回复列表
    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        //非空校验
        if(articleId==null || articleId<=0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //调用DAO
        List<ArticleReply> result = articleReplyMapper.selectByArticleId(articleId);
        //返回结果
        return result;
    }

    //新增一条回复记录
    @Override
    public void create(ArticleReply articleReply) {
        //非空校验
        if(articleReply==null || StringUtils.isEmpty(articleReply.getContent())
        || articleReply.getArticleId()==null || articleReply.getArticleId()<=0
        || articleReply.getPostUserId()==null || articleReply.getPostUserId()<=0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_BANNED));
        }
        //查询帖子对象
        Article article = articleService.selectById(articleReply.getArticleId());
        //校验帖子是否存在
        if(article==null || article.getDeleteState()==1){
            //打印日志
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //校验帖子是否封贴
        if(article.getState()==1){
            //打印日志
            log.warn(ResultCode.FAILED_ARTICLE_STATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_STATE));
        }
        //构建默认值
        articleReply.setLikeCount(0);  //点赞数
        articleReply.setState((byte) 0);  //状态
        articleReply.setDeleteState((byte) 0);  //是否删除
        Date date=new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);

        //调用DAO，写入数据库
        int articleReplyRow = articleReplyMapper.insertSelective(articleReply);

        if(articleReplyRow!=1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //更新帖子的回复数量
        Article updateArticle=new Article();
        updateArticle.setId(article.getId());
        updateArticle.setReplyCount(article.getReplyCount()+1);  //设置回复数量
        updateArticle.setUpdateTime(new Date());

        //调用article的service完成更新操作
        articleService.updateById(updateArticle);

        //打印日志
        log.info(ResultCode.SUCCESS.toString()+"回复发布成功，articleId="+article.getId());
    }
}
